package com.loubobooo.personal.blog.service.impl;

import com.loubobooo.personal.blog.entity.Comment;
import com.loubobooo.personal.blog.entity.vo.ArticleVO;
import com.loubobooo.personal.blog.entity.vo.CommentListVo;
import com.loubobooo.personal.blog.entity.vo.CommentVO;
import com.loubobooo.personal.blog.mapper.CommentMapper;
import com.loubobooo.personal.blog.mapper.custom.ArticleMapperCustom;
import com.loubobooo.personal.blog.mapper.custom.CommentMapperCustom;
import com.loubobooo.personal.blog.page.PageResult;
import com.loubobooo.personal.blog.service.CommentService;
import com.loubobooo.personal.blog.util.Functions;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService{
	@Autowired
	private CommentMapperCustom commentMapperCustom;

	@Autowired
	private CommentMapper commentMapper;

	@Autowired
	private ArticleMapperCustom articleMapperCustom;

	@Override
	public void insertComment(HttpServletRequest request, Comment comment)  {
	    String ip = Functions.getIpAddr(request);
	    comment.setCommentIp(ip);
		commentMapper.insertSelective(comment);
	}
	
	@Override
	public List<CommentVO> listCommentByArticleId(Integer status,Integer articleId) {
		List<CommentVO> commentVOList = commentMapperCustom.listCommentByArticleId(status,articleId);
		return commentVOList;
	}

	@Override
	public CommentVO getCommentById(Integer id)  {
		CommentVO commentVO = new CommentVO();
		Comment comment = commentMapper.selectByPrimaryKey(id);
		BeanUtils.copyProperties(comment, commentVO);
		return commentVO;
	}

	/**
	 * @Description: TODO:等待修改的内容
	 * @return     返回类型
	 * @date 2018/3/23 15:43
	 */
	@Override
	public List<CommentListVo> listCommentByPage(Integer status, Integer pageNo, Integer pageSize)  {
		List<CommentListVo> commentListVoList = new ArrayList<CommentListVo>();

		PageResult pageResult = null;
		int totalCount = commentMapperCustom.countComment(status);
		if (pageNo != null) {
			pageResult = new PageResult(totalCount, pageNo,pageSize);
		} else {
			pageResult = new PageResult(totalCount, 1,pageSize);
		}
		List<CommentVO> commentVOList = commentMapperCustom.listCommentByPage(status,pageResult.getStartPos(), pageSize);

		for(int i = 0; i< commentVOList.size(); i++) {
			CommentListVo commentListVo = new CommentListVo();
			//获得文章信息
			Integer articleId = commentVOList.get(i).getCommentArticleId();
			ArticleVO articleVO = articleMapperCustom.getArticleById(status,articleId);
			commentListVo.setArticleVO(articleVO);

			//评论信息
            CommentVO commentVO = commentVOList.get(i);
			//评论者Gravatar头像
            String avatar = Functions.getGravatar(commentVO.getCommentAuthorEmail());
            commentVO.setCommentAuthorAvatar(avatar);
            commentListVo.setCommentVO(commentVOList.get(i));

			commentListVoList.add(commentListVo);
		}

		if(CollectionUtils.isNotEmpty(commentListVoList)) {
			//4、将Page信息存储在第一个元素中
			commentListVoList.get(0).setPageResult(pageResult);
		}
		return commentListVoList;
	}

	/**
	  * @Description: TODO:等待修改的内容
	  * @return     返回类型
	  * @date 2018/3/23 15:43
	  */
	@Override
	public List<CommentListVo> listCommentVo(Integer status)  {
		List<CommentListVo> commentListVoList = new ArrayList<CommentListVo>();

		List<CommentVO> commentVOList = commentMapperCustom.listComment(status);

		for(int i = 0; i< commentVOList.size(); i++) {
			CommentListVo commentListVo = new CommentListVo();
			//获得文章信息
			Integer articleId = commentVOList.get(i).getCommentArticleId();
			ArticleVO articleVO = articleMapperCustom.getArticleById(status,articleId);
			commentListVo.setArticleVO(articleVO);

			//评论信息
			CommentVO commentVO = commentVOList.get(i);
			//评论者Gravatar头像
			String avatar = Functions.getGravatar(commentVO.getCommentAuthorEmail());
			commentVO.setCommentAuthorAvatar(avatar);
			commentListVo.setCommentVO(commentVOList.get(i));

			commentListVoList.add(commentListVo);
		}

		return commentListVoList;
	}

	@Override
	public List<CommentVO> listComment(Integer status)  {
		List<CommentVO> commentVOList = commentMapperCustom.listComment(status);
		return commentVOList;
	}

	@Override
	public void deleteComment(Integer id)  {
		commentMapper.deleteByPrimaryKey(id);
	}

	@Override
	public void updateComment(Comment comment)  {
		commentMapper.updateByPrimaryKeySelective(comment);
	}

	@Override
	public Integer countComment(Integer status)  {
		Integer commentCount = commentMapperCustom.countComment(status);
		return commentCount;
	}

	/**
	  * @Description: TODO:
	  * @return     返回类型
	  * @date 2018/3/23 16:22
	  */
	@Override
	public List<CommentListVo> listRecentComment(Integer limit)  {
		List<CommentListVo> recentCommentList = new ArrayList<CommentListVo>();
		List<CommentVO> commentVOList = commentMapperCustom.listRecentComment(limit);
		for(int i = 0; i< commentVOList.size(); i++) {
			CommentListVo commentListVo = new CommentListVo();
			//给每个评论用户添加头像
			String avatar = Functions.getGravatar(commentVOList.get(i).getCommentAuthorEmail());
			CommentVO commentVO = commentVOList.get(i);
			commentVO.setCommentAuthorAvatar(avatar);
			commentListVo.setCommentVO(commentVO);
			//找到评论对应的文章信息
			ArticleVO articleVO = articleMapperCustom.getArticleById(1, commentVO.getCommentArticleId());
			commentListVo.setArticleVO(articleVO);

			recentCommentList.add(commentListVo);
		}

		return recentCommentList;
	}

	@Override
	public List<Comment> listChildComment(Integer id)  {
		List<Comment> childCommentList = commentMapperCustom.listChildComment(id);
		return childCommentList;
	}

}
